home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / DirectX / DX8 Fix / d3dx8_0b.exe / SkinnedMesh.h < prev   
Encoding:
C/C++ Source or Header  |  2001-03-12  |  9.5 KB  |  424 lines

  1. #pragma once
  2.  
  3. #ifndef __MVIEW_H__
  4. #define __MVIEW_H__
  5.  
  6.  
  7.  
  8. /*//////////////////////////////////////////////////////////////////////////////
  9. //
  10. // File: mview.h
  11. //
  12. // Copyright (C) 2000-2001 Microsoft Corporation. All Rights Reserved.
  13. //
  14. //
  15. //////////////////////////////////////////////////////////////////////////////*/
  16.  
  17. #define GXRELEASE(_p) do { if ((_p) != NULL) {(_p)->Release(); (_p) = NULL;} } while (0)
  18.  
  19. enum METHOD {
  20.     D3DNONINDEXED,
  21.     D3DINDEXED,
  22.     SOFTWARE,
  23.     NONE
  24. };
  25.  
  26. struct SMeshContainer
  27. {
  28.     ID3DXMesh *pMesh;
  29.     ID3DXMesh* pMeshSW;
  30.     D3DMATERIAL8 *rgMaterials;
  31.     LPDIRECT3DTEXTURE8 *pTextures;
  32.     DWORD cpattr;
  33.     DWORD cMaterials;
  34.     DWORD iAttrSplit;
  35.  
  36.     SMeshContainer *pmcNext;
  37.  
  38.     char *szName;
  39.  
  40.     // Skin info
  41.     LPD3DXSKINMESH m_pSkinMesh;
  42.     D3DXATTRIBUTERANGE *m_pAttrTable;
  43.     D3DXMATRIX** m_pBoneMatrix;
  44.     LPD3DXBUFFER m_pBoneNamesBuf;
  45.     LPD3DXBUFFER m_pBoneOffsetBuf;
  46.     D3DXMATRIX* m_pBoneOffsetMat;
  47.     DWORD* m_rgiAdjacency;
  48.     DWORD m_numBoneComb;
  49.     DWORD m_maxFaceInfl;
  50.     LPD3DXBUFFER m_pBoneCombinationBuf;
  51.     METHOD  m_Method;
  52.     DWORD m_paletteSize;
  53.     BOOL m_bUseSW;
  54.  
  55.     SMeshContainer()
  56.         :pMesh(NULL),
  57.             pMeshSW(NULL),
  58.             rgMaterials(NULL),
  59.             pTextures(NULL),
  60.             cpattr(0),
  61.             iAttrSplit(0),
  62.             cMaterials(0),
  63.             pmcNext(NULL),
  64.             szName(NULL),
  65.             m_pSkinMesh(NULL),
  66.             m_pAttrTable(NULL),
  67.             m_pBoneMatrix(NULL),
  68.             m_pBoneNamesBuf(NULL),
  69.             m_pBoneOffsetBuf(NULL),
  70.             m_pBoneOffsetMat(NULL),
  71.             m_rgiAdjacency(NULL),
  72.             m_numBoneComb(0),
  73.             m_maxFaceInfl(0),
  74.             m_pBoneCombinationBuf(NULL),
  75.             m_Method(NONE),
  76.             m_paletteSize(0),
  77.             m_bUseSW(FALSE)
  78.     {
  79.     }
  80.  
  81.     ~SMeshContainer()
  82.     {
  83.         delete []rgMaterials;
  84.  
  85.         if (pTextures)
  86.         {
  87.             for (DWORD i = 0; i < cMaterials; ++i)
  88.             {
  89.                 GXRELEASE(pTextures[i]);
  90.             }
  91.             delete []pTextures;
  92.         }
  93.  
  94.         GXRELEASE(pMesh);
  95.         GXRELEASE(pMeshSW);
  96.         GXRELEASE(m_pSkinMesh);
  97.         GXRELEASE(m_pBoneNamesBuf);
  98.         GXRELEASE(m_pBoneOffsetBuf);
  99.         GXRELEASE(m_pBoneCombinationBuf);
  100.  
  101.         delete[] m_pBoneMatrix;
  102.  
  103.         delete[] m_pAttrTable;
  104.  
  105.         delete []szName;
  106.  
  107.         delete[] m_rgiAdjacency;
  108.  
  109.         delete pmcNext;
  110.     }
  111. };
  112.  
  113. // X File formation rotate key
  114. struct SRotateKeyXFile
  115. {
  116.     DWORD            dwTime;
  117.     DWORD            dwFloats;    
  118.     float            w;
  119.     float            x;
  120.     float            y;
  121.     float            z;
  122. };
  123.  
  124. struct SScaleKeyXFile
  125. {
  126.     DWORD    dwTime;
  127.     DWORD    dwFloats;    
  128.     D3DXVECTOR3    vScale;    
  129. };
  130.  
  131.  
  132. struct SPositionKeyXFile
  133. {
  134.     DWORD    dwTime;
  135.     DWORD    dwFloats;    
  136.     D3DXVECTOR3    vPos;    
  137. };
  138.  
  139. struct SMatrixKeyXFile
  140. {
  141.     DWORD    dwTime;
  142.     DWORD    dwFloats;    
  143.     D3DXMATRIX    mat;    
  144. };
  145.  
  146. // in memory versions
  147.  
  148. struct SRotateKey
  149. {
  150.     DWORD            dwTime;
  151.     D3DXQUATERNION    quatRotate;    
  152. };
  153.  
  154. struct SPositionKey
  155. {
  156.     DWORD    dwTime;
  157.     D3DXVECTOR3    vPos;    
  158. };
  159.  
  160. struct SScaleKey
  161. {
  162.     DWORD    dwTime;
  163.     D3DXVECTOR3    vScale;    
  164. };
  165.  
  166. struct SMatrixKey
  167. {
  168.     DWORD    dwTime;
  169.     D3DXMATRIX    mat;    
  170. };
  171.  
  172. struct SFrame
  173. {
  174.     SMeshContainer *pmcMesh;
  175.     D3DXMATRIX matRot;
  176.     D3DXMATRIX matTrans;
  177.     D3DXMATRIX matRotOrig;
  178.     D3DXMATRIX matCombined;
  179.  
  180.     // animation information
  181.     SPositionKey *m_pPositionKeys;
  182.     UINT m_cPositionKeys;
  183.     SRotateKey *m_pRotateKeys;
  184.     UINT m_cRotateKeys;
  185.     SScaleKey *m_pScaleKeys;
  186.     UINT m_cScaleKeys;
  187.     SMatrixKey *m_pMatrixKeys;
  188.     UINT m_cMatrixKeys;
  189.  
  190.     SFrame *pframeAnimNext;
  191.     SFrame *pframeToAnimate;
  192.  
  193.     SFrame *pframeSibling;
  194.     SFrame *pframeFirstChild;
  195.  
  196.     bool bAnimationFrame;
  197.     char *szName;
  198.  
  199.     SFrame()
  200.         :
  201.             pmcMesh(NULL),
  202.             m_pPositionKeys(NULL),
  203.             m_cPositionKeys(0),
  204.             m_pScaleKeys(NULL),
  205.             m_cScaleKeys(0),
  206.             m_pRotateKeys(NULL),
  207.             m_cRotateKeys(0),
  208.             m_pMatrixKeys(NULL),
  209.             m_cMatrixKeys(0),
  210.             pframeAnimNext(NULL),
  211.             pframeToAnimate(NULL),
  212.             pframeSibling(NULL),
  213.             pframeFirstChild(NULL),
  214.             bAnimationFrame(false),
  215.             szName(NULL)
  216.     {
  217.         D3DXMatrixIdentity(&matRot);
  218.         D3DXMatrixIdentity(&matRotOrig);
  219.         D3DXMatrixIdentity(&matTrans);
  220.     }
  221.  
  222.     ~SFrame()
  223.     {
  224.         delete []szName;
  225.         delete pmcMesh;        
  226.         delete pframeFirstChild;
  227.         delete pframeSibling;
  228.  
  229.         delete []m_pPositionKeys;
  230.         delete []m_pRotateKeys;
  231.         delete []m_pScaleKeys;
  232.         delete []m_pMatrixKeys;
  233.  
  234.         // do NOT delete pframeAnimNext
  235.         // do NOT delete pframeToAnimate
  236.     }
  237.  
  238.     void SetTime(float fTime);
  239.  
  240.     SFrame *FindFrame(char *szFrame)
  241.     {
  242.         SFrame *pframe;
  243.  
  244.         if ((szName != NULL) && (strcmp(szName, szFrame) == 0))
  245.             return this;
  246.  
  247.         if (pframeFirstChild != NULL)
  248.         {
  249.             pframe = pframeFirstChild->FindFrame(szFrame);
  250.             if (pframe != NULL)
  251.                 return pframe;
  252.         }
  253.  
  254.         if (pframeSibling != NULL)
  255.         {
  256.             pframe = pframeSibling->FindFrame(szFrame);
  257.             if (pframe != NULL)
  258.                 return pframe;
  259.         }
  260.  
  261.         return NULL;
  262.     }
  263.  
  264.     void ResetMatrix()
  265.     {
  266.         matRot = matRotOrig;
  267.         D3DXMatrixIdentity(&matTrans);        
  268.  
  269.         if (pframeFirstChild != NULL)
  270.         {
  271.             pframeFirstChild->ResetMatrix();
  272.         }
  273.  
  274.         if (pframeSibling != NULL)
  275.         {
  276.             pframeSibling->ResetMatrix();
  277.         }
  278.     }
  279.  
  280.     void AddFrame(SFrame *pframe)
  281.     {
  282.         if (pframeFirstChild == NULL)
  283.         {
  284.             pframeFirstChild = pframe;
  285.         }
  286.         else
  287.         {
  288.             pframe->pframeSibling = pframeFirstChild->pframeSibling;
  289.             pframeFirstChild->pframeSibling = pframe;
  290.         }
  291.     }
  292.  
  293.     void AddMesh(SMeshContainer *pmc)
  294.     {
  295.         pmc->pmcNext = pmcMesh;
  296.         pmcMesh = pmc;
  297.     }
  298. };
  299.  
  300. struct SDrawElement
  301. {
  302.     SFrame *pframeRoot;
  303.  
  304.     D3DXVECTOR3 vCenter;
  305.     float fRadius;
  306.  
  307.     // name of element for selection purposes
  308.     char *szName;
  309.  
  310.     // animation list
  311.     SFrame *pframeAnimHead;
  312.  
  313.     // next element in list
  314.     SDrawElement *pdeNext;
  315.  
  316.     float fCurTime;
  317.     float fMaxTime;
  318.  
  319.     SDrawElement()
  320.         :vCenter(0.0,0.0,0.0),
  321.             fRadius(1.0),
  322.             szName(NULL),
  323.             pframeRoot(NULL),
  324.             pframeAnimHead(NULL),
  325.             pdeNext(NULL)
  326.     {
  327.     }
  328.  
  329.     ~SDrawElement()
  330.     {
  331.         delete pframeRoot;
  332.         delete pdeNext;
  333.         delete [] szName;
  334.  
  335.         // do NOT delete pframeAnimHead;
  336.     }
  337.  
  338.     void AddAnimationFrame(SFrame *pframeAnim)
  339.     {
  340.         pframeAnim->pframeAnimNext = pframeAnimHead;
  341.         pframeAnimHead = pframeAnim;
  342.     }
  343.  
  344.     SFrame *FindFrame(char *szName)
  345.     {
  346.         if (pframeRoot == NULL)
  347.             return NULL;
  348.         else
  349.             return pframeRoot->FindFrame(szName);
  350.     }
  351. };
  352.  
  353. HRESULT CalculateBoundingSphere(SDrawElement *pdeCur);
  354.  
  355.  
  356.  
  357. extern DWORD      g_dwNumDeviceTypes;
  358. //-----------------------------------------------------------------------------
  359. // Name: class CMyD3DApplication
  360. // Desc: Application class. The base class (CD3DApplication) provides the 
  361. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  362. //       adds functionality specific to this sample program.
  363. //-----------------------------------------------------------------------------
  364. class CMyD3DApplication : public CD3DApplication
  365. {
  366.     CD3DFont* m_pFont;
  367.  
  368.     METHOD m_method;
  369.  
  370.     DWORD m_dwFVF;
  371.  
  372.     CD3DArcBall   m_ArcBall;
  373.  
  374.     SMeshContainer *m_pmcSelectedMesh;
  375.     SFrame *m_pframeSelected;
  376.     SDrawElement *m_pdeSelected;
  377.     SDrawElement *m_pdeHead;
  378.  
  379.     TCHAR m_szPath[MAX_PATH];
  380.     LPD3DXMATRIX m_pBoneMatrices;
  381.     DWORD m_maxBones;
  382.  
  383. protected:
  384.     HRESULT OneTimeSceneInit();
  385.     HRESULT InitDeviceObjects();
  386.     HRESULT RestoreDeviceObjects();
  387.     HRESULT InvalidateDeviceObjects();
  388.     HRESULT DeleteDeviceObjects();
  389.     HRESULT Render();
  390.     HRESULT FrameMove();
  391.     HRESULT FinalCleanup();
  392.     HRESULT ConfirmDevice(D3DCAPS8*,DWORD,D3DFORMAT);
  393.  
  394.     HRESULT FindBones(SFrame *pframeCur, SDrawElement *pde);
  395.     HRESULT LoadMeshHierarchy();
  396.     HRESULT LoadAnimationSet(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  397.                              DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  398.                              SFrame *pframeParent);
  399.     HRESULT LoadAnimation(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  400.                           DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  401.                           SFrame *pframeParent);
  402.     HRESULT LoadFrames(LPDIRECTXFILEDATA pxofobjCur, SDrawElement *pde,
  403.                        DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  404.                        SFrame *pframeParent);
  405.     HRESULT LoadMesh(LPDIRECTXFILEDATA pxofobjCur,
  406.                      DWORD options, DWORD fvf, LPDIRECT3DDEVICE8 pD3DDevice,
  407.                      SFrame *pframeParent);
  408.     HRESULT SetProjectionMatrix();
  409.     HRESULT DeleteSelectedMesh();
  410.     HRESULT DrawMeshContainer(SMeshContainer *pmcMesh);
  411.     HRESULT UpdateFrames(SFrame *pframeCur, D3DXMATRIX &matCur);
  412.     HRESULT GenerateMesh(SMeshContainer* pmcMesh);
  413.     HRESULT DrawFrames(SFrame *pframeCur, UINT &cTriangles);
  414.  
  415. public:
  416.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  417.     CMyD3DApplication();
  418.  
  419. };
  420.  
  421.  
  422. #endif
  423.  
  424.